Home:ALL Converter>Downloading multiple files with requests in Python

Downloading multiple files with requests in Python

Ask Time:2020-02-24T01:21:15         Author:black_devil

Json Formatter

Currently im facing following problem:

I have 3 download links in a list. Only the last file in the list is downloaded completely. The others have a file size of one kilobyte.

Code:

from requests import get

def download(url, filename):
    with open(filename, "wb") as file:
        response = get(url, stream=True)
        file.write(response.content)

for link in f:
    url = link
    split_url = url.split("/")
    filename = split_url[-1]
    filename = filename.replace("\n", "")
    download(url,filename)

The result looks like this:

Result

How do I make sure that all files are downloaded correctly? All links are direct download links.

Thanks in advance!

EDIT:

I discovered it only happens when I read the links from the .txt

If I create the list in python like this:

links = ["http://ipv4.download.thinkbroadband.com/20MB.zip",
            "http://ipv4.download.thinkbroadband.com/10MB.zip",
            "http://ipv4.download.thinkbroadband.com/5MB.zip"]

... the problem doesnt appear.

reproduceable example:

from requests import get

def download(url, filename):
    with open(filename, "wb") as file:
        response = get(url, stream = True)
        file.write(response.content)

f = open('links.txt','r')
for link in f:
    url = link
    split_url = url.split("/")
    filename = split_url[-1]
    filename = filename.replace("\n", "")
    download(url,filename)

content of links.txt:

http://ipv4.download.thinkbroadband.com/20MB.zip
http://ipv4.download.thinkbroadband.com/10MB.zip
http://ipv4.download.thinkbroadband.com/5MB.zip

Author:black_devil,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/60364827/downloading-multiple-files-with-requests-in-python
yy